/*
 ******************************************************************************
 *
 *       Copyright ASTRI, 2016.  All rights reserved.
 *
 *   This software is proprietary to and embodies the confidential technology
 *   of Hong Kong Applied Science and Technology Research Institute Company
 *   Limited (ASTRI).
 *   Possession, use, or copying of this software and media is authorized
 *   only pursuant to a valid written license from ASTRI or an authorized
 *   sublicensor.
 *
 * File Name:
 *   XXX.java
 *
 * Purpose:
 *   
 *   
 *   
 *
 * Project or Modules Name:
 *   
 *
 * Modules Contained:
 *   N/A
 *
 * Modification History:
 * Date(DDMMYY)  Version No Modified By  Description
 * ------------  ---------- -----------  -----------
 * 20160405       0.0.0.1    XXX     Created
 *
 ******************************************************************************
 */


package com.j.thetabk.thetas;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by user on 14/4/2016.
 */
public class ThetaSProtocol {

    public static String sessionid="sessionId";
    public static String support="Support";
    public static String name="name";
    public static String parameters="parameters";
    public static String results="results";

    public static JSONObject ShootStillImageParam(String sid) throws JSONException {
        JSONObject jbtemp2=new JSONObject();
        jbtemp2.put(sessionid,sid);
        JSONObject jbShootstillimage=new JSONObject();
        jbShootstillimage.put(name,"camera.takePicture");
        jbShootstillimage.put(parameters,jbtemp2);
        return jbShootstillimage;
    }

    public static JSONObject CaptureVideoParam(String sid) throws JSONException {
        JSONObject jbtemp2=new JSONObject();
        jbtemp2.put(sessionid,sid);
        JSONObject jbShootstillimage=new JSONObject();
        jbShootstillimage.put(name,"camera._startCapture");
        jbShootstillimage.put(parameters,jbtemp2);
        return jbShootstillimage;
    }

    public static JSONObject StopVideoParam(String sid) throws JSONException {
        JSONObject jbtemp2=new JSONObject();
        jbtemp2.put(sessionid,sid);
        JSONObject jbShootstillimage=new JSONObject();
        jbShootstillimage.put(name,"camera._stopCapture");
        jbShootstillimage.put(parameters,jbtemp2);
        return jbShootstillimage;
    }

    public static JSONObject LivePreviewParam(String sid) throws JSONException {
        JSONObject jbtemp2=new JSONObject();
        jbtemp2.put(sessionid,sid);
        JSONObject jbShootstillimage=new JSONObject();
        jbShootstillimage.put(name,"camera._getLivePreview");
        jbShootstillimage.put(parameters,jbtemp2);
        return jbShootstillimage;
    }

    public static JSONObject CheckUpdateParam(String fpt) throws JSONException {
        JSONObject jbConfirmfilesave=new JSONObject();
        jbConfirmfilesave.put("stateFingerprint",fpt);
        return jbConfirmfilesave;
    }

    public static JSONObject StartSessionParam() throws JSONException {
        JSONObject jbStartsession=new JSONObject();
        jbStartsession.put(name,"camera.startSession");
        jbStartsession.put(parameters, new JSONObject());
        return jbStartsession;
    }


    public static JSONObject CloseSessionParam(String sessionId) throws JSONException {
        JSONObject jbClosesession=new JSONObject();
        jbClosesession.put(name,"camera.closeSession");
        JSONObject jbtemp=new JSONObject();
        jbtemp.put(sessionid,sessionId);
        jbClosesession.put(parameters, jbtemp);
        return jbClosesession;
    }


    public static JSONObject SetPropertyParam(JSONObject jbPItem, String sessionId) throws JSONException {
        JSONObject jbSetProperties1=new JSONObject();
        jbSetProperties1.put(name,"camera.setOptions");
        JSONObject jbtemp=new JSONObject();
        jbtemp.put(sessionid,sessionId);
        jbtemp.put("options",jbPItem);
        jbSetProperties1.put(parameters, jbtemp);
        return jbSetProperties1;
    }


    public static JSONObject AcquirePropertyParam(JSONArray jaProperties, String sessionId) throws JSONException {
        JSONObject jbtemp2= new JSONObject();
        jbtemp2.put(sessionid,sessionId);
        jbtemp2.put("optionNames",jaProperties);
        JSONObject jbAcquireProperty=new JSONObject();
        jbAcquireProperty.put(name,"camera.getOptions");
        jbAcquireProperty.put(parameters, jbtemp2);
        return jbAcquireProperty;
    }

    public static boolean IsStateDone(JSONObject response) throws JSONException {
        String status = response.getString("state");
        if (status.equals("done"))
            return true;
        return false;
    }


    public static String GetSessionId(JSONObject jsonObject) throws JSONException {
        return jsonObject.getJSONObject(results).getString(sessionid);
    }

    public static JSONObject CheckStatusParam(String commandId) throws JSONException {
        JSONObject input = new JSONObject();
        input.put("id", commandId);
        return input;
    }

    public static String GetFileUri(JSONObject jsonObject) throws JSONException {
        return jsonObject.getJSONObject(results).getString("fileUri");
    }


    public static JSONObject GetProperties(JSONObject response) throws JSONException {
        return response.getJSONObject(results).getJSONObject("options");
    }

//    public static JSONObject AcquirePropertyParam(int [] indexes, String sessionId) throws JSONException {
//        JSONObject jbtemp2= new JSONObject();
//        jbtemp2.put(sessionid,sessionId);
//
//        JSONArray jaProperties=new JSONArray();
//        AddPropertyName(jaProperties,indexes);
//
//        jbtemp2.put("optionNames",jaProperties);
//        JSONObject jbAcquireProperty=new JSONObject();
//        jbAcquireProperty.put("name","camera.getOptions");
//        jbAcquireProperty.put("parameters", jbtemp2);
//        return jbAcquireProperty;
//    }

    public static String [] StringListFromArray(JSONArray ja) throws JSONException {
        String  [] eplist;
        eplist=new String[ja.length()];
        for(int i=0;i<ja.length();i++)
            eplist[i]=ja.getString(i);
        return eplist;
    }

    public static String [] GetPropertyList(JSONObject jbProperties, String propertyName) throws JSONException {
        JSONArray jatemp = jbProperties.getJSONArray(propertyName+support);
        return StringListFromArray(jatemp);
    }



    /*



    public static JSONObject PropertyItem(JSONObject jbProperties, int index, int choice) throws JSONException {
        JSONObject jbtemp2=new JSONObject();
        AddPropertyItem(jbtemp2, jbProperties, index, choice);
        return jbtemp2;
    }

    public static void AddPropertyItem(JSONObject jbinout, JSONObject jbProperties, int index, int choice) throws JSONException {
        jbinout.put(property[index], jbProperties.getJSONArray(property[index] + support).get(choice));
    }












    public static String GetProperty(JSONObject jbProperties, int index) throws JSONException {
        return jbProperties.getString(property[index]);
    }




    public static JSONObject AcquirePropertySupportParam(int [] indexes, String sessionId) throws JSONException {
        JSONObject jbtemp2= new JSONObject();
        jbtemp2.put(sessionid,sessionId);

        JSONArray jaProperties=new JSONArray();
        AddPropertyNameSupport(jaProperties, indexes);

        jbtemp2.put("optionNames",jaProperties);
        JSONObject jbAcquireProperty=new JSONObject();
        jbAcquireProperty.put("name","camera.getOptions");
        jbAcquireProperty.put("parameters", jbtemp2);
        return jbAcquireProperty;
    }







    public static JSONObject AcquirePropertyAndSupportParam(int [] indexes, String sessionId) throws JSONException {
        JSONObject jbtemp2= new JSONObject();
        jbtemp2.put(sessionid,sessionId);

        JSONArray jaProperties=new JSONArray();
        AddPropertyNameSupport(jaProperties, indexes);
        AddPropertyName(jaProperties, indexes);

        jbtemp2.put("optionNames",jaProperties);
        JSONObject jbAcquireProperty=new JSONObject();
        jbAcquireProperty.put("name","camera.getOptions");
        jbAcquireProperty.put("parameters", jbtemp2);
        return jbAcquireProperty;
    }

*/
}
